General project set-up

# Libraries
  library(reshape2)
  library(tidyverse)

# Graphs

theme_set (theme_classic() + theme(panel.grid.major = element_blank(),
                              panel.grid.minor = element_blank(), 
                              axis.line = element_line(colour = "black"),
                              legend.position="none",
                              axis.text.x = element_text(angle = 90, vjust = 0.5),
                              plot.title = element_text(size=12, face="bold"),
                              #panel.border = element_rect(colour = "black", fill=NA, size=1)
                              panel.border = element_blank()
                              ))

    
# Data    
  # Fragment list
  #CurrentFragments<-read.csv("8.Metadata/Frag_assignments.csv", header = T)

  # Collection information
  #Location<-read.csv("8.Metadata/Genotypes.csv", header = T)

Meta data

# Treatment Info
  Treatment<-read.csv("Data/Treatments.csv")
  #duplicated(Treatment$Tag)
  Treatment<-Treatment[!duplicated(Treatment$Tag), ]
  #duplicated(Treatment$Tag)
  summary(Treatment)
##       Tag          Genotype     Nutrients       Disease         Available  
##  201    :  1   Elkhorn : 29   Ambient:129   Dead    :  1   Dead      : 10  
##  202    :  1   Kelsey-1: 26   Dead   :  1   Extra   :  6   Experiment:246  
##  204    :  1   U44     : 26   Extra  :  6   Pathogen:136   Extra?    :  7  
##  205    :  1   Acerv2  : 25   NH4    :126   Placebo :119                   
##  207    :  1   FM19    : 25   TL     :  1   TL      :  1                   
##  208    :  1   FM6     : 25                                                
##  (Other):257   (Other) :107                                                
##      A_Tank        Blast_Tank  
##  Min.   :1.000   Min.   :1.00  
##  1st Qu.:3.000   1st Qu.:2.00  
##  Median :4.500   Median :4.00  
##  Mean   :4.504   Mean   :4.37  
##  3rd Qu.:6.000   3rd Qu.:6.00  
##  Max.   :8.000   Max.   :8.00  
##  NA's   :17      NA's   :17
# Tags weight
  Tags<-read.csv("Data/Tags_W2.csv")
  #duplicated(Tags$Tag)
  Tags<-Tags[!duplicated(Tags$Tag), ]
  Tags<-Tags %>% select(T_Type, Tag, T_AW, T_BW, Tag_Vol.cm3.)
  #duplicated(Tags$Tag)
  summary(Tags)
##  T_Type       Tag           T_AW            T_BW        Tag_Vol.cm3.   
##  B:200   201    :  1   Min.   :10.01   Min.   :1.393   Min.   : 8.401  
##  S: 77   202    :  1   1st Qu.:10.14   1st Qu.:1.443   1st Qu.: 8.524  
##          204    :  1   Median :16.26   Median :2.285   Median :13.672  
##          205    :  1   Mean   :14.56   Mean   :2.050   Mean   :12.250  
##          207    :  1   3rd Qu.:16.31   3rd Qu.:2.298   3rd Qu.:13.726  
##          208    :  1   Max.   :16.48   Max.   :2.333   Max.   :13.865  
##          (Other):271
  # Tags$T_SW_den<-(999.842594+0.06793952*(Tags$Temperature)-0.00909529*(Tags$Temperature)^2+0.0001001685* (Tags$Temperature)^3-0.000001120083*(Tags$Temperature)^4+0.000000006536332*(Tags$Temperature)^5+(0.824493-0.0040899*(Tags$Temperature)+0.000076438*(Tags$Temperature)^2-0.00000082467*(Tags$Temperature)^3+0.0000000053875* (Tags$Temperature)^4)*(Tags$Salinity)+(-0.00572466+0.00010227*(Tags$Temperature)-0.0000016546*(Tags$Temperature)^2)* (Tags$Salinity)^1.5+0.00048314*(Tags$Salinity)^2)*0.001
  
   #Tags$Estimated_Density<-(-Tags$SW_den/((Tags$T_BW/Tags$T_AW)-1))
   
  Tags %>%
      group_by(T_Type) %>%
      summarise_at(vars(T_AW, T_BW, Tag_Vol.cm3.), funs(mean(., na.rm=TRUE)))

BW data

# 1. BW data
  BW_Tall<-read.csv("Data/BW_ Long_data.csv")
  #BW_Tall<-read.csv("Data/BW_ Long_data2.csv")  
  #BW_Tall$Estimated.W.density[BW_Tall$Estimated.W.density == "#VALUE!" ] <-NA
  #BW_Tall$Estimated.W.density<-as.numeric(as.character(BW_Tall$Estimated.W.density))
  
  #BW_Tall$Estimated_AW [BW_Tall$Estimated_AW == "#VALUE!" ] <-NA
  #BW_Tall$Estimated_AW<-as.numeric(as.character(BW_Tall$Estimated_AW))
  
# 2. Data clean-up an types: 
  
  # Variable types 
    #BW_Tall$Time <- as.factor(BW_Tall$Time)
    #BW_Tall$Time<-as.numeric(BW_Tall$Time)
    BW_Tall$Date<-as.Date(BW_Tall$Date, "%Y-%m-%d")
    BW_Tall$Day<-(as.numeric(BW_Tall$Date)-18486)
  
  # Remove-unused data    
    #Extras <- BW_Tall[which (BW_Tall$Nutrients=="Extra"), ]
    #BW_Tall <- droplevels(BW_Tall[!rownames(BW_Tall) %in%
    #                                   rownames(Extras), ])

# 3. Merge with treatments
    BW_Tall<-plyr::join(BW_Tall, Treatment, by = "Tag", 
                type = "left", match = "all")

    BW_Tall<-plyr::join(BW_Tall, Tags, by = "Tag", 
                type = "left", match = "all")
    
    BW_Tall$Nutrients<-factor(BW_Tall$Nutrients, 
                             levels= c("Ambient", "NH4"), ordered=TRUE)
    BW_Tall$Disease<-factor(BW_Tall$Disease, 
                             levels= c("Placebo", "Pathogen"), ordered=TRUE)

# 4. Replicates
    BW_Tall$Tank<-factor(BW_Tall$Tank, ordered=FALSE)
    BW_Tall$Genotype<-factor(BW_Tall$Genotype, ordered=FALSE)

    summary(BW_Tall)
##       Tag            Tank          Date                  BW        
##  201    :  10   4      :285   Min.   :2020-08-12   Min.   : 2.515  
##  202    :  10   3      :282   1st Qu.:2020-09-04   1st Qu.: 4.009  
##  204    :  10   2      :280   Median :2020-09-18   Median : 4.793  
##  205    :  10   6      :279   Mean   :2020-09-18   Mean   : 5.320  
##  207    :  10   1      :276   3rd Qu.:2020-10-09   3rd Qu.: 6.353  
##  208    :  10   7      :276   Max.   :2020-10-15   Max.   :11.950  
##  (Other):2431   (Other):813   NA's   :3            NA's   :4       
##       Temp            Sal      
##  Min.   :26.10   Min.   :31.5  
##  1st Qu.:27.00   1st Qu.:32.2  
##  Median :27.40   Median :33.2  
##  Mean   :27.35   Mean   :33.1  
##  3rd Qu.:27.70   3rd Qu.:34.0  
##  Max.   :29.10   Max.   :34.7  
##  NA's   :3       NA's   :3     
##                                                                Notes     
##                                                                   :2414  
##  new to the experiment                                            :   6  
##  New                                                              :   5  
##  water sample 3                                                   :   3  
##  Broke in 3 pieces 10-14-2020                                     :   2  
##  BW from 10-09 probably wrong. I think it is supposed to be 8.3398:   2  
##  (Other)                                                          :  59  
##       Day            Genotype      Nutrients        Disease    
##  Min.   : 0.00   FM19    : 247   Ambient:1235   Placebo :1155  
##  1st Qu.:23.00   Kelsey-1: 242   NH4    :1241   Pathogen:1321  
##  Median :37.00   U44     : 242   NA's   :  15   NA's    :  15  
##  Mean   :37.74   FM6     : 241                                 
##  3rd Qu.:58.00   Acerv2  : 240                                 
##  Max.   :64.00   (Other) :1276                                 
##  NA's   :3       NA's    :   3                                 
##       Available        A_Tank        Blast_Tank     T_Type          T_AW      
##  Dead      :  39   Min.   :1.000   Min.   :1.000   B   :1760   Min.   :10.01  
##  Experiment:2438   1st Qu.:3.000   1st Qu.:2.000   S   : 728   1st Qu.:10.14  
##  Extra?    :  11   Median :5.000   Median :4.000   NA's:   3   Median :16.25  
##  NA's      :   3   Mean   :4.516   Mean   :4.373               Mean   :14.47  
##                    3rd Qu.:6.000   3rd Qu.:6.000               3rd Qu.:16.31  
##                    Max.   :8.000   Max.   :8.000               Max.   :16.48  
##                    NA's   :53      NA's   :53                  NA's   :3      
##       T_BW        Tag_Vol.cm3.   
##  Min.   :1.393   Min.   : 8.401  
##  1st Qu.:1.440   1st Qu.: 8.519  
##  Median :2.284   Median :13.666  
##  Mean   :2.037   Mean   :12.172  
##  3rd Qu.:2.298   3rd Qu.:13.725  
##  Max.   :2.333   Max.   :13.865  
##  NA's   :3       NA's   :3

Water density BW tag corrections

# 1. Water density

  BW_Tall$SW_den<-(999.842594+0.06793952*(BW_Tall$Temp)-0.00909529*(BW_Tall$Temp)^2+0.0001001685*
    (BW_Tall$Temp)^3-0.000001120083*(BW_Tall$Temp)^4+0.000000006536332*(BW_Tall$Temp)^5+(0.824493-0.0040899*
    (BW_Tall$Temp)+0.000076438*(BW_Tall$Temp)^2-0.00000082467*(BW_Tall$Temp)^3+0.0000000053875*(BW_Tall$Temp)^4)*
    (BW_Tall$Sal)+(-0.00572466+0.00010227*(BW_Tall$Temp)-0.0000016546*(BW_Tall$Temp)^2)*
    (BW_Tall$Sal)^1.5+0.00048314*(BW_Tall$Sal)^2)*0.001

# 2. Tag_temperature_salinity_specific BW
  BW_Tall$T_BW_S <-BW_Tall$T_AW-(BW_Tall$SW_den * BW_Tall$Tag_Vol.cm3.)

# 3. Coral BW
  BW_Tall$Coral_BW<-((BW_Tall$BW)-(BW_Tall$T_BW_S))

BW calculations

BW.data<-BW_Tall[order(BW_Tall$Tag), ]

# 4. Calculate days bw BW data points
BW.data<-BW.data %>%
  group_by(Tag) %>%
  dplyr::mutate(Days = Day - lag(Day, default = Day[1]))

# 5. Calculate change in BW bw data points

BW.data<-BW.data %>%
  group_by(Tag) %>%
  dplyr::mutate(dBW = Coral_BW - lag(Coral_BW, default = Coral_BW[1]))

BW.data$dBW[which(BW.data$Tag=="A129"& BW.data$Date=="2020-10-02")]<-NA
BW.data$dBW[which(BW.data$Tag=="A017"& BW.data$Date=="2020-09-18")]<-NA

BW.data<-BW.data %>%
    group_by(Tag) %>%
    dplyr::mutate(dBW_r = dBW/(Days*lag(Coral_BW, default = Coral_BW[1])))
  
  

  
  BW.data$dBW_r<-BW.data$dBW_r*1000
  
  summary(BW.data)
##       Tag            Tank          Date                  BW        
##  201    :  10   4      :285   Min.   :2020-08-12   Min.   : 2.515  
##  202    :  10   3      :282   1st Qu.:2020-09-04   1st Qu.: 4.009  
##  204    :  10   2      :280   Median :2020-09-18   Median : 4.793  
##  205    :  10   6      :279   Mean   :2020-09-18   Mean   : 5.320  
##  207    :  10   1      :276   3rd Qu.:2020-10-09   3rd Qu.: 6.353  
##  208    :  10   7      :276   Max.   :2020-10-15   Max.   :11.950  
##  (Other):2431   (Other):813   NA's   :3            NA's   :4       
##       Temp            Sal      
##  Min.   :26.10   Min.   :31.5  
##  1st Qu.:27.00   1st Qu.:32.2  
##  Median :27.40   Median :33.2  
##  Mean   :27.35   Mean   :33.1  
##  3rd Qu.:27.70   3rd Qu.:34.0  
##  Max.   :29.10   Max.   :34.7  
##  NA's   :3       NA's   :3     
##                                                                Notes     
##                                                                   :2414  
##  new to the experiment                                            :   6  
##  New                                                              :   5  
##  water sample 3                                                   :   3  
##  Broke in 3 pieces 10-14-2020                                     :   2  
##  BW from 10-09 probably wrong. I think it is supposed to be 8.3398:   2  
##  (Other)                                                          :  59  
##       Day            Genotype      Nutrients        Disease    
##  Min.   : 0.00   FM19    : 247   Ambient:1235   Placebo :1155  
##  1st Qu.:23.00   Kelsey-1: 242   NH4    :1241   Pathogen:1321  
##  Median :37.00   U44     : 242   NA's   :  15   NA's    :  15  
##  Mean   :37.74   FM6     : 241                                 
##  3rd Qu.:58.00   Acerv2  : 240                                 
##  Max.   :64.00   (Other) :1276                                 
##  NA's   :3       NA's    :   3                                 
##       Available        A_Tank        Blast_Tank     T_Type          T_AW      
##  Dead      :  39   Min.   :1.000   Min.   :1.000   B   :1760   Min.   :10.01  
##  Experiment:2438   1st Qu.:3.000   1st Qu.:2.000   S   : 728   1st Qu.:10.14  
##  Extra?    :  11   Median :5.000   Median :4.000   NA's:   3   Median :16.25  
##  NA's      :   3   Mean   :4.516   Mean   :4.373               Mean   :14.47  
##                    3rd Qu.:6.000   3rd Qu.:6.000               3rd Qu.:16.31  
##                    Max.   :8.000   Max.   :8.000               Max.   :16.48  
##                    NA's   :53      NA's   :53                  NA's   :3      
##       T_BW        Tag_Vol.cm3.        SW_den          T_BW_S     
##  Min.   :1.393   Min.   : 8.401   Min.   :1.020   Min.   :1.387  
##  1st Qu.:1.440   1st Qu.: 8.519   1st Qu.:1.021   1st Qu.:1.446  
##  Median :2.284   Median :13.666   Median :1.021   Median :2.286  
##  Mean   :2.037   Mean   :12.172   Mean   :1.021   Mean   :2.043  
##  3rd Qu.:2.298   3rd Qu.:13.725   3rd Qu.:1.022   3rd Qu.:2.306  
##  Max.   :2.333   Max.   :13.865   Max.   :1.022   Max.   :2.360  
##  NA's   :3       NA's   :3        NA's   :3       NA's   :3      
##     Coral_BW           Days             dBW                dBW_r       
##  Min.   : 0.408   Min.   : 0.000   Min.   :-0.043590   Min.   :-6.716  
##  1st Qu.: 1.764   1st Qu.: 6.000   1st Qu.: 0.000552   1st Qu.: 1.003  
##  Median : 2.687   Median : 7.000   Median : 0.028408   Median : 1.908  
##  Mean   : 3.278   Mean   : 6.381   Mean   : 0.041328   Mean   : 2.106  
##  3rd Qu.: 4.247   3rd Qu.: 7.000   3rd Qu.: 0.057556   3rd Qu.: 3.059  
##  Max.   :10.523   Max.   :14.000   Max.   : 0.350371   Max.   :12.416  
##  NA's   :4        NA's   :3        NA's   :7           NA's   :515
  BW.data<-BW.data[which(BW.data$Available!="Dead"),]
  BW.data<-BW.data[which(BW.data$Available!="Extra?"),]

AW calculations analysis

# 6. Coral air weight
  BW.data$Est_Coral_AW<-(BW.data$Coral_BW)*(1/(1-(BW.data$SW_den)/2.4))
  
# 7. Calculate change in AW bw data points
BW.data<-BW.data %>%
    dplyr::group_by(Tag) %>%
    dplyr::mutate(dAW = Est_Coral_AW - lag(Est_Coral_AW, default = Est_Coral_AW[1]))
  
  BW.data$dAW[which(BW.data$Tag=="A129"& BW.data$Date=="2020-10-02")]<-NA
  BW.data$dAW[which(BW.data$Tag=="A017"& BW.data$Date=="2020-09-18")]<-NA

BW.data<-BW.data %>%
  group_by(Tag) %>%
  dplyr::mutate(dAW_d = dAW/Days)

BW.data<-BW.data %>%
  group_by(Tag) %>%
  dplyr::mutate(dAW_r = dAW/(Days*lag(Est_Coral_AW, default = Est_Coral_AW[1])))

BW.data$dAW_r<-BW.data$dAW_r*1000

summary(BW.data)
##       Tag            Tank          Date                  BW        
##  201    :  10   4      :285   Min.   :2020-08-12   Min.   : 2.515  
##  202    :  10   2      :277   1st Qu.:2020-09-04   1st Qu.: 4.019  
##  204    :  10   1      :274   Median :2020-09-25   Median : 4.810  
##  205    :  10   5      :274   Mean   :2020-09-19   Mean   : 5.330  
##  207    :  10   6      :274   3rd Qu.:2020-10-09   3rd Qu.: 6.360  
##  208    :  10   3      :270   Max.   :2020-10-15   Max.   :11.888  
##  (Other):2378   (Other):784                        NA's   :1       
##       Temp            Sal       
##  Min.   :26.10   Min.   :31.50  
##  1st Qu.:27.00   1st Qu.:32.20  
##  Median :27.40   Median :33.20  
##  Mean   :27.34   Mean   :33.09  
##  3rd Qu.:27.70   3rd Qu.:34.00  
##  Max.   :29.10   Max.   :34.70  
##                                 
##                                                                Notes     
##                                                                   :2368  
##  new to the experiment                                            :   4  
##  water sample 3                                                   :   3  
##  Broke in 3 pieces 10-14-2020                                     :   2  
##  BW from 10-09 probably wrong. I think it is supposed to be 8.3398:   2  
##  pale                                                             :   2  
##  (Other)                                                          :  57  
##       Day            Genotype     Nutrients        Disease    
##  Min.   : 0.00   FM19    :247   Ambient:1211   Placebo :1137  
##  1st Qu.:23.00   FM14    :240   NH4    :1227   Pathogen:1301  
##  Median :44.00   FM6     :240                                 
##  Mean   :38.17   U44     :240                                 
##  3rd Qu.:58.00   Kelsey-1:239                                 
##  Max.   :64.00   Acerv2  :237                                 
##                  (Other) :995                                 
##       Available        A_Tank        Blast_Tank    T_Type        T_AW      
##  Dead      :   0   Min.   :1.000   Min.   :1.000   B:1717   Min.   :10.01  
##  Experiment:2438   1st Qu.:3.000   1st Qu.:2.000   S: 721   1st Qu.:10.14  
##  Extra?    :   0   Median :5.000   Median :4.000            Median :16.25  
##                    Mean   :4.516   Mean   :4.373            Mean   :14.45  
##                    3rd Qu.:6.000   3rd Qu.:6.000            3rd Qu.:16.31  
##                    Max.   :8.000   Max.   :8.000            Max.   :16.48  
##                                                                            
##       T_BW        Tag_Vol.cm3.        SW_den          T_BW_S     
##  Min.   :1.393   Min.   : 8.401   Min.   :1.020   Min.   :1.387  
##  1st Qu.:1.440   1st Qu.: 8.519   1st Qu.:1.021   1st Qu.:1.445  
##  Median :2.284   Median :13.665   Median :1.021   Median :2.287  
##  Mean   :2.034   Mean   :12.156   Mean   :1.021   Mean   :2.040  
##  3rd Qu.:2.298   3rd Qu.:13.725   3rd Qu.:1.022   3rd Qu.:2.306  
##  Max.   :2.333   Max.   :13.865   Max.   :1.022   Max.   :2.360  
##                                                                  
##     Coral_BW           Days             dBW               dBW_r       
##  Min.   : 0.408   Min.   : 0.000   Min.   :-0.04359   Min.   :-6.716  
##  1st Qu.: 1.786   1st Qu.: 6.000   1st Qu.: 0.00122   1st Qu.: 0.986  
##  Median : 2.706   Median : 7.000   Median : 0.02833   Median : 1.882  
##  Mean   : 3.290   Mean   : 6.379   Mean   : 0.04109   Mean   : 2.078  
##  3rd Qu.: 4.270   3rd Qu.: 7.000   3rd Qu.: 0.05699   3rd Qu.: 3.010  
##  Max.   :10.446   Max.   :14.000   Max.   : 0.35037   Max.   :12.416  
##  NA's   :1                         NA's   :4          NA's   :495     
##   Est_Coral_AW          dAW                dAW_d             dAW_r        
##  Min.   : 0.7097   Min.   :-0.077175   Min.   :-0.0110   Min.   :-6.7871  
##  1st Qu.: 3.1083   1st Qu.: 0.003817   1st Qu.: 0.0049   1st Qu.: 0.9518  
##  Median : 4.7084   Median : 0.048208   Median : 0.0086   Median : 1.8469  
##  Mean   : 5.7274   Mean   : 0.071708   Mean   : 0.0103   Mean   : 2.0802  
##  3rd Qu.: 7.4270   3rd Qu.: 0.099988   3rd Qu.: 0.0140   3rd Qu.: 3.0176  
##  Max.   :18.1770   Max.   : 0.610562   Max.   : 0.0556   Max.   :12.5749  
##  NA's   :1         NA's   :4           NA's   :495       NA's   :495
#write.csv(BW.data, "BW.data.csv", row.names = FALSE)

BW.CRF<-BW.data[which(BW.data$Genotype=='K2'|
                      BW.data$Genotype=='U41'|
                      BW.data$Genotype=='U44'), ]

BW.UM<-BW.data[which(BW.data$Genotype=='Acerv2'|
                      BW.data$Genotype=='Cooper-9'|
                      BW.data$Genotype=='Elkhorn'|
                      BW.data$Genotype=='Kelsey-1'), ]

BW.FWC<-BW.data[which(BW.data$Genotype=='FM14'|
                      BW.data$Genotype=='FM6'|
                      BW.data$Genotype=='FM19'|
                      BW.data$Genotype=='FM9'), ]

BW Exploration

SizeBiasCheck<- ggplot(BW.data, aes (BW, dBW_r, colour=factor(Date))) +
  #geom_smooth(method = "lm")+
  geom_jitter(alpha=0.5) + 
  scale_y_continuous(limits = c(0, 5),
                         breaks = seq(0, 6,2),  
                         expand = c(0, 0),
                         name=("rBW (mg / g*day)"))
SizeBiasCheck

SizeBiasCheck<- ggplot(BW.data, aes (BW, dBW_r, colour=factor(Genotype))) +
  #geom_smooth(method = "lm")+
  geom_jitter(alpha=0.5) + 
  scale_y_continuous(limits = c(0, 15),
                         breaks = seq(0, 15,2),  
                         expand = c(0, 0),
                         name=("rBW (mg / g*day)"))
SizeBiasCheck

Genotype

BW_Genet<- ggplot(BW.data, aes (Date, Coral_BW, colour=factor(Genotype))) +
  stat_summary(fun.data = "mean_cl_boot",geom = "errorbar", width = 0.2 )+
  stat_summary(fun.y=mean, geom="line") + 
  geom_jitter(alpha=0.5) +
  scale_y_continuous(limits = c(0, 12),
                         breaks = seq(0, 15,1),  
                         expand = c(0, 0),
                         name=("BW [g]"))+
  theme(legend.position = "bottom")

BW_Genet

BW_Genet+facet_grid(~Nutrients)

BW_Genet+facet_grid(Disease~Nutrients)

dBW_Genet<- ggplot(BW.data, aes (Date, dBW, colour=factor(Genotype))) +
  stat_summary(fun.data = "mean_cl_boot",geom = "errorbar", width = 0.2 )+
  stat_summary(fun.y=mean, geom="line") + 
  geom_jitter(alpha=1) +
  #scale_y_continuous(limits = c(-1.4, 1.5),
   #                      breaks = seq(-1.4, 1.5, 0.4),  
    #                     expand = c(0, 0),
     #                    name=("dBW [g]"))+
  theme(legend.position = "bottom")
  
dBW_Genet

dBW_Genet+ facet_wrap(~Nutrients)

dBW_Genet + facet_wrap(Nutrients~Disease)

dBWr_Genet<- ggplot(BW.data, aes (Date, dBW_r, colour=factor(Genotype))) +
  geom_jitter(alpha=0.3) +
  stat_summary(fun.data = "mean_cl_boot",geom = "errorbar", width = 0.2 )+
  stat_summary(fun.y=mean, geom="line") + 
    #scale_y_continuous(limits = c(-2, 6),
     #                    breaks = seq(-2, 10, 1),  
      #                   expand = c(0, 0),
       #                  name=("dBW [mg / g*day]"))+
  theme(legend.position = "bottom")
  
dBWr_Genet

dBWr_Genet+ facet_wrap(~Nutrients)

dBWr_Genet + facet_wrap(Nutrients~Disease)

Pre-treatment bias

BW_Bias<- ggplot(BW.data, aes (Genotype, dBW_r, colour=factor(Nutrients))) +
  geom_jitter(alpha=0.3) +
  stat_summary(fun.data = "mean_cl_boot",geom = "errorbar", width = 0.2 )+
  stat_summary(fun.y=mean, geom="line") + 
  
  scale_y_continuous(limits = c(0, 5),
                         breaks = seq(0, 5,1),  
                         expand = c(0, 0),
                         name=("dBW [mg / g*day]"))
BW_Bias+facet_grid(~Date)

AW Exploration

SizeBiasCheck<- ggplot(BW.data, aes (Est_Coral_AW, dAW_r, colour=factor(Genotype))) +
  #stat_summary(fun.data = "mean_cl_boot",geom = "errorbar", width = 0.2 )+
  #stat_summary(fun.y=mean, geom="line") + 
  geom_jitter(alpha=0.5) + 
  scale_y_continuous(limits = c(0, 10),
                         breaks = seq(0, 10,2),  
                         expand = c(0, 0),
                         name=("dAW [mg / g*day]"))
ggExtra::ggMarginal(
  p = SizeBiasCheck,
  type = 'density',
  margins = 'both',
  size = 5,
  colour = 'black',
  fill = 'gray'
)

Genotype

AW_Genet<- ggplot(BW.data, aes (Date, Est_Coral_AW, colour=factor(Genotype))) +
  stat_summary(fun.data = "mean_cl_boot",geom = "errorbar", width = 0.2 )+
  stat_summary(fun.y=mean, geom="line") + 
  geom_jitter(alpha=0.5) +
  scale_y_continuous(limits = c(0, 20),
                         breaks = seq(0, 20,1),  
                         expand = c(0, 0),
                         name=("estimated AW (g)"))+
  theme(legend.position = "bottom")

AW_Genet

AW_Genet+facet_grid(~Nutrients)

AW_Genet+facet_grid(Disease~Nutrients)

dAW_Genet<- ggplot(BW.data, aes (Date, dAW, colour=factor(Genotype))) +
  stat_summary(fun.data = "mean_cl_boot",geom = "errorbar", width = 0.2 )+
  stat_summary(fun.y=mean, geom="line") + 
  theme(legend.position = "none")+
  geom_jitter(alpha=0.5) + theme(legend.position = "bottom")+
  scale_y_continuous(limits = c(-0.18, 0.7),
                         breaks = seq(-1, 1,0.1),  
                         expand = c(0, 0),
                         name=("dAW (g)"))
  
dAW_Genet

dAW_Genet+ facet_wrap(~Nutrients)

dAW_Genet+ facet_wrap(~Disease)

dAW_Gd<- ggplot(BW.data, aes (Date, dAW_d*1000, colour=factor(Genotype))) +
  stat_summary(fun.data = "mean_cl_boot",geom = "errorbar", width = 0.2 )+
  stat_summary(fun.y=mean, geom="line") + 
  theme(legend.position = "none")+
  geom_jitter(alpha=0.5) + theme(legend.position = "bottom")+
  scale_y_continuous(limits = c(-15, 60),
                          breaks = seq(-15, 60, 5),  
                         expand = c(0, 0),
                        name=("dAW (mg/d)"))
  
dAW_Gd

dAW_Gd+ facet_wrap(~Nutrients)

dAW_Gd+ facet_wrap(~Disease)

dAWr_Genet<- ggplot(BW.data, aes (Date, dAW_r, colour=factor(Genotype))) +
  stat_summary(fun.data = "mean_cl_boot",geom = "errorbar", width = 0.2 )+
  stat_summary(fun.y=mean, geom="line") + 
  theme(legend.position = "none")+
  geom_jitter(alpha=0.5) + theme(legend.position = "bottom")+
  scale_y_continuous(limits = c(0, 10),
                         breaks = seq(0, 8,2),  
                         expand = c(0, 0),
                         name=("AW (mg / g*day)"))
  
dAWr_Genet

dAWr_Genet+ facet_wrap(~Nutrients)

dAWr_Genet+ facet_wrap(~Disease)

dAW_Genet2<- ggplot(BW.data, aes (Date, dAW, colour=Genotype)) +
  geom_vline(xintercept = as.Date("2020-08-12"), linetype=3)+
  geom_vline(xintercept = as.Date("2020-08-21"), linetype=3)+
  geom_vline(xintercept = as.Date("2020-09-04"), linetype=3)+
  geom_vline(xintercept = as.Date("2020-09-11"), linetype=3)+
  geom_vline(xintercept = as.Date("2020-09-14"), linetype=4)+
  geom_vline(xintercept = as.Date("2020-09-18"), linetype=3)+
  geom_vline(xintercept = as.Date("2020-09-25"), linetype=3)+
  geom_vline(xintercept = as.Date("2020-10-02"), linetype=3)+
  geom_vline(xintercept = as.Date("2020-10-09"), linetype=3)+
  
  stat_summary(fun.data = "mean_cl_boot",geom = "errorbar", width = 0.2 )+
  stat_summary(fun.y=mean, geom="line") + 
  geom_vline(xintercept = as.Date("2020-09-14"), linetype=3)+
  theme(legend.position = "none")+
  geom_jitter(shape=21, alpha=0.3)+
  scale_y_continuous(limits = c(-.2, 0.5),
                        breaks = seq(0, 1,0.05),  
                         expand = c(0, 0),
                         name=("Growth (mg / g d)"))+
  scale_x_date(limits = c(as.Date("2020-08-20"), as.Date("2020-10-20")),
                         breaks = "7 day",
                         expand = c(0, 0),
                         name=("Growth (mg / g d)"))+
  
  theme(legend.position = "bottom")
 
dAW_Genet2

dAW_Genet2 + facet_wrap(~Nutrients)

dAW_Genet2 + facet_wrap(~Tank)

dAWr_Genet2<- ggplot(BW.data, aes (Date, dAW_r, colour=Genotype)) +
  geom_vline(xintercept = as.Date("2020-08-12"), linetype=3)+
  geom_vline(xintercept = as.Date("2020-08-21"), linetype=3)+
  geom_vline(xintercept = as.Date("2020-09-04"), linetype=3)+
  geom_vline(xintercept = as.Date("2020-09-11"), linetype=3)+
  geom_vline(xintercept = as.Date("2020-09-14"), linetype=4)+
  geom_vline(xintercept = as.Date("2020-09-18"), linetype=3)+
  geom_vline(xintercept = as.Date("2020-09-25"), linetype=3)+
  geom_vline(xintercept = as.Date("2020-10-02"), linetype=3)+
  geom_vline(xintercept = as.Date("2020-10-09"), linetype=3)+
  
  stat_summary(fun.data = "mean_cl_boot",geom = "errorbar", width = 0.2 )+
  stat_summary(fun.y=mean, geom="line") + 
  geom_vline(xintercept = as.Date("2020-09-14"), linetype=3)+
  theme(legend.position = "none")+
  #geom_jitter(shape=21, alpha=0.3)+
  scale_y_continuous(limits = c(0, 5),
                         breaks = seq(0, 10,0.5),  
                         expand = c(0, 0),
                         name=("Growth (mg / g d)"))+
  scale_x_date(limits = c(as.Date("2020-08-20"), as.Date("2020-10-20")),
                         breaks = "7 day",
                         expand = c(0, 0),
                         name=("Growth (mg / g d)"))+
  
  theme(legend.position = "bottom")
 
dAWr_Genet2

dAWr_Genet2 + facet_wrap(~Nutrients)

dAWr_Genet2 + facet_wrap(~Tank)

BW_Frag<- ggplot(BW.data, aes (Date, dAW_r, group=(Tag),
                                    colour=factor(Genotype))) + 
      geom_line()+
      geom_hline(yintercept = 0, linetype=3)+
      
      #scale_x_continuous(name="Days", 
       #                  breaks = seq(0, 30, by=15)) +
      scale_y_continuous(name="Growth (mg / g d)", 
                         limits = c(-2, 10),
                         breaks = seq(-1, 10, by=1)) +
    
    theme(legend.position="bottom",
        legend.title = element_blank(), 
        strip.background =element_rect(fill=NA)) 
BW_Frag+facet_wrap(Nutrients~Genotype)

Tank

AW_Tank<- ggplot(BW.data, aes (Tank, dAW_r)) +
  stat_summary(fun.data = "mean_cl_boot",geom = "errorbar", width = 0.2 )+
  stat_summary(fun.y=mean, geom="line") + 
  scale_y_continuous(limits = c(0, 10),
                         breaks = seq(0, 10,2),  
                         expand = c(0, 0),
                         name=("Growth rate (mg / g day)"))+
  geom_jitter(aes(colour=Genotype), shape=21, alpha=0.3)+
  facet_wrap(~Date) +
  theme(legend.position = "bottom")
AW_Tank

#AW_Tank+ facet_wrap(Date~Nutrients)
#AW_Tank+ facet_wrap(Date~Disease)

AW_Disease<- ggplot(BW.data, aes (Date, dAW_r, colour=Genotype)) +
  stat_summary(fun.data = "mean_cl_boot",geom = "errorbar", width = 0.2 )+
  stat_summary(fun.y=mean, geom="line") + 
  scale_y_continuous(limits = c(-3, 10),
                         breaks = seq(-3, 10,2),  
                         expand = c(0, 0),
                         name=("Growth rate (mg/g day)"))+
  geom_jitter(alpha=0.3)+
  theme(legend.position = "bottom")
AW_Disease

Treatments

AW<- ggplot(BW.data, aes (Date, Est_Coral_AW, colour=Genotype)) +
  stat_summary(fun.data = "mean_cl_boot",geom = "errorbar", width = 0.2 )+
  stat_summary(fun.y=mean, geom="line") + 
  scale_y_continuous(limits = c(-3, 10),
                         breaks = seq(-3, 10,2),  
                         expand = c(0, 0),
                         name=("Estimated AW (g)"))+
  geom_jitter(alpha=0.3, shape=21)+
  theme(legend.position = "bottom")
AW

AW + facet_grid(Disease~Nutrients)

D_AW<- ggplot(BW.data, aes (Date, dAW, colour=Genotype)) +
  stat_summary(fun.data = "mean_cl_boot",geom = "errorbar", width = 0.2 )+
  stat_summary(fun.y=mean, geom="line") + 
  scale_y_continuous(limits = c(-2, 2),
                         breaks = seq(-2, 2, 0.2),  
                         expand = c(0, 0),
                         name=("Growth (g)"))+
  geom_jitter(alpha=0.3)+
  theme(legend.position = "bottom")
D_AW

D_AW + facet_wrap(~Nutrients)

D_AW + facet_wrap(Nutrients~Disease)

D_AW<- ggplot(BW.data, aes (Date, dAW_r, colour=Genotype)) +
  stat_summary(fun.data = "mean_cl_boot",geom = "errorbar", width = 0.2 )+
  stat_summary(fun.y=mean, geom="line") + 
  scale_y_continuous(limits = c(-3, 10),
                         breaks = seq(-3, 10,2),  
                         expand = c(0, 0),
                         name=("Growth rate (mg/g day)"))+
  geom_jitter(alpha=0.3, shape=21)+
  theme(legend.position = "bottom")
D_AW

D_AW + facet_wrap(~Nutrients)

D_AW + facet_wrap(~Disease)

Fragments

AW_Disease<- ggplot(BW.data, aes (Date, dAW_r, group=Tag, 
                                  colour=Nutrients)) +
  stat_summary(fun.data = "mean_cl_boot",geom = "errorbar", width = 0.2 )+
  stat_summary(fun.y=mean, geom="line") + 
  scale_y_continuous(limits = c(-3, 10),
                         breaks = seq(-3, 10,2),  
                         expand = c(0, 0),
                         name=("Growth rate (mg/g day)"))+
  geom_jitter(alpha=0.3)+
  theme(legend.position = "bottom")
AW_Disease

AW_Disease + facet_wrap(~Tank)

AW_Disease + facet_wrap(Nutrients~Disease)

Pre-treatment bias

AW_Bias<- ggplot(BW.data, aes (Date, dAW_r, colour=factor(Genotype), shape=(Nutrients))) +
  stat_summary(fun.data = "mean_cl_boot",geom = "errorbar", width = 0.2 )+
  stat_summary(fun.y=mean, geom="line") + 
  geom_jitter(alpha=0.5) +
  scale_y_continuous(limits = c(0, 5),
                         breaks = seq(0, 5,1),  
                         expand = c(0, 0),
                         name=("dBW [mg / g*day]"))
AW_Bias + facet_grid(~Disease)+
  theme(legend.position = "bottom")

#Summary<-BW.data %>% group_by(Genotype, Nutrients, Disease) %>% count(Date)
#BW_Tall2<-BW.data %>% group_by(Disease, Genotype, Nutrients) %>% add_count(Date)
#write.csv(Summary, "numbers.csv")

Packages used

# Creates bibliography 
#knitr::write_bib(c(.packages()), "packages.bib")

Henry, Lionel, and Hadley Wickham. 2019. Purrr: Functional Programming Tools. https://CRAN.R-project.org/package=purrr.

Müller, Kirill, and Hadley Wickham. 2019. Tibble: Simple Data Frames. https://CRAN.R-project.org/package=tibble.

R Core Team. 2020. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.

Wickham, Hadley. 2017a. Reshape2: Flexibly Reshape Data: A Reboot of the Reshape Package. https://CRAN.R-project.org/package=reshape2.

———. 2017b. Tidyverse: Easily Install and Load the ’Tidyverse’. https://CRAN.R-project.org/package=tidyverse.

———. 2019a. Forcats: Tools for Working with Categorical Variables (Factors). https://CRAN.R-project.org/package=forcats.

———. 2019b. Stringr: Simple, Consistent Wrappers for Common String Operations. https://CRAN.R-project.org/package=stringr.

Wickham, Hadley, Winston Chang, Lionel Henry, Thomas Lin Pedersen, Kohske Takahashi, Claus Wilke, Kara Woo, and Hiroaki Yutani. 2019. Ggplot2: Create Elegant Data Visualisations Using the Grammar of Graphics. https://CRAN.R-project.org/package=ggplot2.

Wickham, Hadley, Romain François, Lionel Henry, and Kirill Müller. 2019. Dplyr: A Grammar of Data Manipulation. https://CRAN.R-project.org/package=dplyr.

Wickham, Hadley, and Lionel Henry. 2020. Tidyr: Tidy Messy Data. https://CRAN.R-project.org/package=tidyr.

Wickham, Hadley, Jim Hester, and Romain Francois. 2018. Readr: Read Rectangular Text Data. https://CRAN.R-project.org/package=readr.